<template>
  <div>
    <div class='picture-upload-list' v-for="item in uploadList">
      <template v-if="item.status==='finished'">
        <img :src="item.url">
        <div class="picture-upload-list-cover">
          <Icon type="ios-eye-outline" @click.native="handleView(item.name)"></Icon>
          <Icon type="ios-trash-outline" @click.native="handleRemove(item)"></Icon>
        </div>
      </template>
      <template v-else>
        <Progress v-if="item.showProgress" :precent="item.percentage" hide-info></Progress>
      </template>
    </div>
    <Upload
      ref="upload"
      :show-upload-list="false"
      :default-file-list="defaultList"
      :on-success="handleSuccess"
      :format="['jpg','jpeg','','png']"
      :max-size="8192"
      :on-format-error="handleFormatError"
      :on-exceeded-size="handleMaxSize"
      :before-upload="handleBeforeUpload"
      multiple
      type="drag"
      action="//jsonplaceholder.typicode.com/posts/"
      style="display: inline-block;width: 28px;">
      <div style="width: 24px;height: 24px;line-height: 24px;">
        <Icon type="camera" size="20"></Icon>
      </div>
    </Upload>
    <Modal title="查看图片" v-model="visible">
      <img :src="'https://o5wwk8baw.qnssl.com/' + imgName + '/large'" v-if="visible" style="width: 100%">
    </Modal>
  </div>
</template>
<script>
  export default {
    props:['companyId'],
    data(){
      return {
        //初始加载的图片
        defaultList: [
          {
            'name': 'a42bdcc1178e62b4694c830f028db5c0',
            'url': 'https://o5wwk8baw.qnssl.com/a42bdcc1178e62b4694c830f028db5c0/avatar'
          },
          {
            'name': 'bc7521e033abdd1e92222d733590f104',
            'url': 'https://o5wwk8baw.qnssl.com/bc7521e033abdd1e92222d733590f104/avatar'
          }
        ],
        uploadList:[],
        visible:false,
        imgName:''
      }
    },
    methods:{
      //查看大图
      handleView(name){
        this.imgName = name;
        this.visible = true;
      },
      //删除上传
      handleRemove(file){
        //前端删除
        const fileList = this.$refs.upload.fileList;
        //从当前的fileList里面删除
        this.$refs.upload.fileList.splice(fileList.indexOf(file),1)
      },
      //成功函数
      handleSuccess(res,file){
        file.url = 'https://ss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/logo/bd_logo1_31bdc765.png'
        file.name ='百度'
      },
      //数据格式错误
      handleFormatError(file){
        this.$Notice.warning({
          title:'文件格式不正确',
          desc:'文件'+file.name+'格式不正确，请上传jpg，jpeg或png格式的图片。'
        })
      },
      //文件大小限制
      handleMaxSize(file){
        this.$Notice.warning({
          title:'超出文件大小限制',
          desc:'文件'+file.name+'太大，不能超多8M。'
        })
      },
      //上传之前的钩子，参数为上传的文件，若放回false或者promis则停止上传
      handleBeforeUpload(){
        //如果当前的默认上传列表的长度小于5
        const check = this.uploadList.length<5
        if(!check){
          this.$Notice.warning({
            title:'最多只能上传5张照片。'
          })
        }
        return check;
      },
      //文件上传成功时的钩子，返回字段为 response, file, fileList
      handleSuccess (res, file) {
        // 因为上传过程为实例，这里模拟添加 url
        file.url = 'https://o5wwk8baw.qnssl.com/7eb99afb9d5f317c912f08b5212fd69a/avatar';
        file.name = '7eb99afb9d5f317c912f08b5212fd69a';
      },
    },
    //钩子函数
    mounted(){
      //页面加载时,默认值是两个图片
      this.uploadList = this.$refs.upload.fileList;
      console.log(this);
      console.log(this.companyId);
    }
  }
</script>
<style>
  .picture-upload-list{
    display: inline-block;
    width: 30px;
    height: 30px;
    text-align: center;
    line-height: 30px;
    border:1px solid transparent;
    border-radius: 4px;
    overflow: hidden;
    background: #fff;
    position: relative;
    box-shadow: 0 1px 1px rgba(0,0,0,.2);
    margin-right: 4px;
  }
  .picture-upload-list img{
    width:100%;
    height: 100%;
  }
  .picture-upload-list-cover{
    display: none;
    position: absolute;
    top:0;
    bottom:0;
    left:0;
    right: 0;
    background: rgba(0,0,0,.6);
  }
  .picture-upload-list:hover .picture-upload-list-cover{
    display: block;
  }
  .picture-upload-list-cover i{
    color:#fff;
    font-size:12px;
    cursor:pointer;
    margin: 0 2px;
  }
</style>
